package com.example.myapp.activities;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.example.myapp.R;
import com.example.myapp.event.*;
import com.example.myapp.models.Game;
import com.example.myapp.models.GameControl;
import com.example.myapp.players.HumanPlayer;
import com.example.myapp.players.RandomSelectBot;
import com.example.myapp.players.SelectFirstBot;
import com.example.myapp.views.CustomGLSurfaceView;

public class MyActivity extends Activity implements
        ClickOnCellEventListener,
        StateMatrixChangeEventListener,
        GameEndEventListener {

    private CustomGLSurfaceView mGLView;
    private static final int HAS_UI = 1;

    private Game game = new Game();
    private GameControl gameControl = new GameControl();

    @Override
    public void clickOnCellEvent(ClickOnCellEvent event) {
//        Game.putPiece(event.getPart(), event.getXi(), event.getYi());
    }

    @Override
    public void stateMatrixOnChange(StateMatrixChangeEvent event) {
        if (HAS_UI == 1) {
            mGLView.handleChanges(game.getStateMatrix());
        }
    }

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        gameControl.setGame(game);
        game.setControl(gameControl);

        EventHandler.register(this, ClickOnCellEvent.class);
        EventHandler.register(this, StateMatrixChangeEvent.class);

        mGLView = new CustomGLSurfaceView(this);

        setContentView(mGLView);

        HumanPlayer player1 = new HumanPlayer(1);
        EventHandler.register(player1, ClickOnCellEvent.class);

        game.getControl().addPlayer(player1);
//        GameControl.addPlayer(new SelectFirstBot(1));
//        game.getControl().addPlayer(new SelectFirstBot(2));
//        game.getControl().addPlayer(new RandomSelectBot(1));
        game.getControl().addPlayer(new RandomSelectBot(2));

        game.initGame();

        new Thread(new Runnable(){
            @Override
            public void run() {
                game.getControl().beginGame();
            }
        }).start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //save status
    }

    @Override
    protected void onResume() {
        super.onResume();
        //restore state or restart
    }

    @Override
    public void onGameEnd(GameEndEvent event) {
        ((TextView) findViewById(R.id.textView)).setText(event.getResult());
    }
}
